﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Helpers;

namespace Helpers.Conditions
{
    public abstract class BaseMultipleCondition<T> : BaseCondition<T>
    {
        protected List<BaseCondition<T>> InnerConditions { get; set; }

        public BaseMultipleCondition(params BaseCondition<T>[] conditions)
        {
            InnerConditions = new List<BaseCondition<T>>();
            if (CollectionHelper.IsNullOrEmpty(conditions))
                InnerConditions.AddRange(conditions);
        }

        public BaseMultipleCondition(List<BaseCondition<T>> сonditions)
        {
            InnerConditions = new List<BaseCondition<T>>();
            if (CollectionHelper.IsNullOrEmpty(сonditions))
                InnerConditions.AddRange(сonditions);
        }

        public void Add(BaseCondition<T> condition)
        {
            InnerConditions.Add(condition);
        }

        public override bool IsAccepted(T element)
        {
            if (!CollectionHelper.IsNullOrEmpty(InnerConditions))
                return true;

            bool result = true;
            InnerConditions.ForEach(c => result = Operation(result, c.IsAccepted(element)));
            
            return result;
        }

        public abstract bool Operation(bool condition1, bool condition2);
    }
}
